package models

import (
	"gorm.io/gorm"
	"yueyue/yue-rpc/handler/config"
)

type User struct {
	gorm.Model
	Mobile   string `json:"mobile"`
	Password string `json:"password"`
}

func Register[T any](data *T) error {
	return config.DB.Create(&data).Error
}
func Login[T any](data *T) error {
	return config.DB.Find(&data).Error
}

type Video struct {
	gorm.Model
	Title     string `json:"title"`
	Message   string `json:"message"`
	Static    string `json:"static"`
	Address   string `json:"address"`
	Image     string `json:"image"`
	ViewCount int    `json:"view_count"`
}

func VideoAdd[T any](data *T) error {
	return config.DB.Create(&data).Error
}
func VideoList[T any](data *T, page, size int, address string) error {
	switch {
	case size > 100:
		size = 100
	case size <= 0:
		size = 10
	}
	offset := (page - 1) * size
	return config.DB.Where("address like ?", "%"+address+"%").Offset(offset).Limit(size).Find(data).Error
}
func VideoDetails[T any](data *T, id int) error {
	return config.DB.Where("id=?", id).Find(&data).Error
}
func VideoType[T any](data *T, static string) error {
	return config.DB.Where("static like ?", static).Find(&data).Error
}

type Episode struct {
	gorm.Model
	Title      string `json:"title"`
	Episode    string `json:"episode"`
	Time       string `json:"time"`
	PlayedTime string `json:"played_time"`
}

func EpisodeList[T any](data *T) error {
	return config.DB.Find(&data).Error
}

type History struct {
	gorm.Model
	VideoId    int    `json:"video_id"`
	EpisodeId  int    `json:"episode_id"`
	Image      string `json:"image"`
	PlayedTime string `json:"played_time"`
}

func HistoryAdd[T any](data *T) error {
	return config.DB.Create(&data).Error
}
func HistoryList[T any](data *T) error {
	return config.DB.Order("created_at desc").Find(&data).Error
}

type Hot struct {
	gorm.Model
	VideoId      int     `json:"video_id"`
	ViewCount    float64 `json:"view_count"`
	LikeCount    float64 `json:"like_count"`
	CollCount    float64 `json:"coll_count"`
	CommentCount float64 `json:"comment_count"`
	HotNum       float64 `json:"hot_num"`
}

func HotCalculate[T any](data *T, VideoId int) error {
	return config.DB.Where("id=?", VideoId).Find(&data).Error
}
func HotList[T any](data *T) error {
	return config.DB.Order("hot_num desc").Limit(50).Find(&data).Error
}
